/**   
* @Title: AbstractProcessor.java
* @Package org.xaoyaoyao.spider.processor
* @Description: TODO
* @author xaoyaoyao
* @date Jan 25, 2019 1:26:01 PM
* @version V1.0   
*/
package org.xaoyaoyao.spider.processor;

import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.xaoyaoyao.spider.enums.ResultEnum;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: AbstractProcessor
 * @Description: TODO
 * @author xaoyaoyao
 * @date Jan 25, 2019 1:26:01 PM
 * 
 */
@Data
@Slf4j
public abstract class AbstractProcessor<T> {

	public AbstractProcessor() {
		super();
	}

	/**
	 * @Title: handle
	 * @Description: 处理业务
	 * @param result
	 * @Reutrn ResultEnum
	 */
	public abstract ResultEnum handle(T result);

	/**
	 * @Title: process
	 * @Description: 处理结果集
	 * @param result
	 * @return ResultEnum
	 */
	public ResultEnum process(T result) {
		log.debug(">>>>>>>> result >> {}", result);
		try {
			return handle(result);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return ResultEnum.ERROR;
		}
	}

	/**
	 * @Title: asyncProcess
	 * @Description: 异步处理结果集
	 * @param result
	 * @return Future<ResultEnum>
	 */
	@Async
	public Future<ResultEnum> asyncProcess(T result) {
		log.info(">>>>>>>> result >> {}", result);
		try {
			handle(result);
			return new AsyncResult<ResultEnum>(ResultEnum.SUCCESS);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return new AsyncResult<ResultEnum>(ResultEnum.ERROR);
		}
	}

}
